# -*- coding:utf-8 -*-
# @Email   : lishuaichao@lingxi.ai
# @Time    : 2023/11/27 15:33
import asyncio
import datetime
import json
import os
import pandas as pd
from bot.insurance_consultant_dx.test.sales_test import Sales

async def execute():
    now_date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    csv_paths = "./测试用例/"
    outputs = []
    if os.path.isdir(csv_paths):
        for file in os.listdir(csv_paths):
            if not file.endswith('.csv'):
                continue
            df = pd.read_csv(csv_paths + file)
            historys = df.to_dict('records')
            output = {'不通过原因':''}
            outputs.append(output)
            output['测试脚本'] = file
            output['测试结论'] = '通过'
            for history in historys:
                history['测试脚本'] = file
                context = history['context']
                content = history['用户输入']
                voice_out = history['输出录音编号']
                voice_expand = history['录音编号变化范围']  if history['录音编号变化范围'] and isinstance(history['录音编号变化范围'], str) else ''
                sales = Sales()
                result = await sales.async_test_reply(json.loads(context), "123", content=content)
                history['AI录音编号'] = result.get('输出录音编号')
                history['AI录音编号变化范围'] = result.get('录音编号变化范围')
                history['AIBOT话术'] = result.get('BOT话术')
                if voice_out == result.get('输出录音编号'):
                    history['录音编号比对'] = '正确'
                else:
                    history['录音编号比对'] = '错误'
                    output['测试结论'] = '不通过'
                    output['不通过原因'] = f"录音编号错误,应输出:{voice_out},实际输出:{result.get('输出录音编号')}"
                if voice_expand == result.get('录音编号变化范围'):
                    history['录音编号变化范围比对'] = '正确'
                else:
                    history['录音编号变化范围比对'] = '错误'
                    output['测试结论'] = '不通过'
                    output['不通过原因'] += f"录音编号变化范围错误,应输出:{voice_expand},实际输出:{result.get('录音编号变化范围')}\n"
            pd.DataFrame(historys).to_csv(f'./测试报告/日志/{now_date}-{file}', index=False, encoding='utf-8')

    pd.DataFrame(outputs).to_csv(f'./测试报告/{now_date}.csv', index=False, encoding='utf-8')


if __name__ == '__main__':
    asyncio.run(execute())
